// pages/rucruit/rucruit.js
import {
  storeList,
} from '../../utils/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    modalShow: false,
    title: '门店入驻',
    err: false,
    region: [],
    name: '',
    address: '',
    phone: '',
    point: '',
    disabled: false,
    modalShow1: false,
    title1: '设备合伙人协议及隐私政策',
    protocol: '设备合伙人协议',
    type: '',
    stores: [],
    modalShow2: false,
    storeName: '',
    storeId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  handleInput(e) {
    let value = e.detail.value
    let type = e.currentTarget.dataset.type
    this.setData({
      [type]: value
    })
  },
  bindRegionChange(e) {
    this.setData({
      region: e.detail.value
    })
  },
  bindLocationChange(e) {
    const that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              wx.showToast({ //这里提示失败原因
                title: '授权成功！',
                duration: 1500
              })
            },
            fail() {
              wx.showToast({
                title: '需要授权位置信息',
                icon: 'none'
              })
            }
          })
        } else {
          wx.chooseLocation({
            success(res) {
              console.log(res);
              that.setData({
                point: res.longitude + ',' + res.latitude
              })
            },
            fail(res) {
              console.log(res);
            }
          })
        }
      }
    })
  },
  modalHide() {},
  cancel() {
    this.setData({
      name: '',
      phone: '',
      region: [],
      point: '',
      address: '',
      modalShow: false,
    })
  },
  confirm() {
    const {
      name,
      phone,
      region,
      point,
      address,
      disabled
    } = this.data
    if (name && phone && region && point && address && !disabled) {

    }
  },
  quruzhu(e) {
    let type = e.currentTarget.dataset.type
    this.setData({
      type
    })
    if (type == 'shop') {
      this.setData({
        modalShow1: true,
        title1: type == 'device' ? '设备合伙人协议及隐私政策' : type == 'consume' ? '消费合伙人协议及隐私政策' : type == 'space' ? '空间站站长协议及隐私政策' : '入驻协议及隐私政策',
        protocol: type == 'device' ? '设备合伙人协议' : type == 'consume' ? '消费合伙人协议' : type == 'space' ? '空间站站长协议' : '入驻协议',
      })
    } else if (type == 'device') {
      wx.navigateTo({
        url: '/pages/sbhhr/index',
      })
    } else if (type == 'consume' || type == 'space') {
      this.setData({
        modalShow2: true
      })
    } else if (type == 'md') {
      wx.navigateTo({
        url: '/pages/sqmd/sqmd?type=' + type
      })
    } else {
      wx.navigateTo({
        url: '/pages/sqxmdz/index',
      })
    }
  },
  cancel1() {
    this.setData({
      modalShow1: false
    })
  },
  confirm1() {
    const {
      type
    } = this.data
    if (type == 'shop') {
      // this.setData({
      //   modalShow: true
      // })
      wx.navigateTo({
        url: '/pages/sqmd/sqmd?type=shop',
      })
    } else if (type == 'device') {
      wx.navigateTo({
        url: '/pages/longPic/index',
      })
    } else if (type == 'consume' || type == 'space') {
      this.setData({
        modalShow2: true
      })
    }
    this.cancel1()
  },
  getList() {
    storeList('').then(res => {
      this.setData({
        stores: res
      })
    })
  },
  bindPickerChange(e) {
    this.setData({
      storeName: this.data.stores[e.detail.value].name,
      storeId: this.data.stores[e.detail.value].id
    })
  },
  cancel3() {
    this.setData({
      modalShow2: false,
      storeId: '',
      storeName: ''
    })
  },
  confirm3() {
    if (this.data.storeId) {
      if (this.data.type == 'space') {
        wx.navigateTo({
          url: '/pages/agreePage/index?type=space&storeId=' + this.data.storeId,
        })
      } else {
        wx.navigateTo({
          url: '/pages/goodsDetail/index?storeId=' + this.data.storeId +  '&id=171',
        })
      }
      this.cancel3()
    } else {
      wx.showToast({
        title: '请选择入驻门店',
        icon: 'none'
      })
    }
  }
})